package com.yzs.base.ui.dialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.animation.Animation
import android.view.animation.RotateAnimation
import android.widget.ImageView
import com.yzs.base.R

/**
 * author : yzs
 * time   : 2022/01/13
 * desc   : 加载等待框
 */
class LoadingDialog(mContext: Context, canCancel: Boolean = false) :
    Dialog(mContext, R.style.LoadingDialog) {

    private var mAnim: RotateAnimation? = null

    init {
        setCanceledOnTouchOutside(canCancel)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_loading_layout)

        findViewById<ImageView>(R.id.iv_close)?.setOnClickListener {
            dismiss()
        }
        initAnim()
    }


    private fun initAnim() {
        mAnim = RotateAnimation(
            0f, 360f, Animation.RESTART, 0.5f,
            Animation.RESTART, 0.5f
        )
        mAnim?.let {
            it.duration = 2000
            it.repeatCount = Animation.INFINITE
            it.repeatMode = Animation.RESTART
            it.startTime = Animation.START_ON_FIRST_FRAME.toLong()
        }
    }

    override fun dismiss() {
        mAnim?.cancel()
        mAnim = null
        super.dismiss()
    }
}